//
//  NewFeatureViewController.swift
//  AAWeibo
//
//  Created by 上官风雨 on 15/11/13.
//  Copyright © 2015年 上官风雨. All rights reserved.
//

import UIKit
import SnapKit

private let reuseIdentifier = "Cell"
private let count = 4
class NewFeatureViewController: UICollectionViewController {
    
    init(){
        
        let layout =  UICollectionViewFlowLayout()
        layout.itemSize = UIScreen.mainScreen().bounds.size
        layout.scrollDirection = .Horizontal
        layout.minimumLineSpacing = 0
        layout.minimumInteritemSpacing = 0
        super.init(collectionViewLayout: layout)
        
        collectionView?.bounces = false
        collectionView?.pagingEnabled = true
        collectionView?.showsHorizontalScrollIndicator = false
        
        
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        self.collectionView!.registerClass(NewFeatureCell.self, forCellWithReuseIdentifier: reuseIdentifier)
        
        
    }
    
    // MARK: UICollectionViewDataSource
    override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        // #warning Incomplete implementation, return the number of items
        return count
    }
    
    override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as! NewFeatureCell
        //        cell.backgroundColor = indexPath.item % 2 == 0 ? UIColor.greenColor() : UIColor.orangeColor()
        
        cell.index = indexPath.item
        
        return cell
    }
    
    
    
}

class NewFeatureCell : UICollectionViewCell {
    
    var index : Int = 0{
        
        didSet{
            
            backImage.image = UIImage(named: "new_feature_\(index + 1)")
            
            if index == 3 {
                
                startBtn.hidden = false
                startAnimation()
            } else{
                startBtn.hidden = true
            }
            
        }
        
    }
    override init(frame: CGRect) {
        
        super.init(frame: frame)
        
        setupUI()
    }
    
    //按钮的点击事件
    func btnDidClick(){
        
        print("开始体验吧...")
        
    }
    //设置子控件
    func setupUI (){
        //背景图片的约束
        contentView.addSubview(backImage)
        contentView.addSubview(startBtn)
        
        backImage.snp_makeConstraints { (make) -> Void in
            
            make.edges.equalTo(contentView.snp_edges)
            
        }
        
        //开始按钮的约束
        startBtn.snp_makeConstraints { (make) -> Void in
            
            make.centerX.equalTo(contentView.snp_centerX)
            make.top.equalTo(contentView.snp_centerY).offset(150)
        }
        
       startBtn.addTarget(self, action: "btnDidClick", forControlEvents: .TouchUpInside)
        
    }
    //开始动画
    func startAnimation (){
        
        startBtn.transform = CGAffineTransformMakeScale(0, 0)
        
        UIView.animateWithDuration(1, delay: 0, usingSpringWithDamping: 0.5, initialSpringVelocity: 5, options: [], animations: { () -> Void in
            
            self.startBtn.transform = CGAffineTransformMakeScale(1, 1)
            
            }) { (_) -> Void in
                
                
        }
        
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    //背景图片的懒加载
    lazy var backImage : UIImageView = UIImageView()
    
    //开始按钮的懒加载
    lazy var startBtn : UIButton = {
        
        let btn = UIButton()
        
        btn.setBackgroundImage(UIImage(named: "new_feature_finish_button"), forState: .Normal)
        btn.setBackgroundImage(UIImage(named: "new_feature_finish_button_highlighted"), forState: .Highlighted)
        btn.setTitle("开始体验", forState: .Normal)
        btn.hidden = true
        return btn
        
    }()
    
}
